package net.maku.newbase.annotation.redis.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import net.maku.newbase.annotation.redis.RedisRW;
import net.maku.newbase.util.redis.RedisUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@Aspect
@Component
public class RedisRWAspect {

    @Autowired
    private RedisUtils redisUtils;

    @Pointcut("@annotation(net.maku.newbase.annotation.redis.RedisRW)")
    public void PointCut() {

    }

    @Before("PointCut()")
    public void Before() {
    }

    @SneakyThrows
    @Around("PointCut()")
    public Object Around(ProceedingJoinPoint pjp) {
        //获取注解参数
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();

        RedisRW redisGet = method.getAnnotation(RedisRW.class);
        Class c = redisGet.c();
        boolean isList = redisGet.isList();
        String tableField = redisGet.tableField();
        String key = redisGet.key();
        if (StringUtils.isBlank(key)) {
            Object[] objects = pjp.getArgs();
            key = String.valueOf(objects[0]);
        }
        key = redisGet.tag() + key;

        //缓存存在
        String value = redisUtils.get(key);
        if (ObjectUtils.isNotEmpty(value)) {
            if (isList) {
                return JSONArray.parseArray(value);
            }
            return JSONObject.parseObject(value, c);
        }

        //缓存不存在
        Object object = pjp.proceed();
        if (ObjectUtils.isNotEmpty(object)
                && StringUtils.isNotBlank(key)
        ) {
            String val;
            if (isList) {
                val = JSONArray.toJSONString(object);
//                if (StringUtils.isNotBlank(tableField)) {
//                    JSONArray jsonArray = JSONArray.parseArray(val);
//                    val = fnEnumData(jsonArray, tableField);
//                    redisUtils.psetex(key, redisUtils.MINUTES * 10, val);
//                    return JSONArray.parseArray(val);
//                }
            } else {
                val = JSONObject.toJSONString(object);
            }
            redisUtils.psetex(key, redisUtils.MINUTES * 10, val);
        }
        return object;
    }

    @After("PointCut()")
    public void After() {
    }

    @AfterReturning("PointCut()")
    public void AfterReturning() {
    }

    /**
     * 存储 枚举字典
     *
     * @param jsonArray
     * @param tableField
     * @return
     */
    public String fnEnumData(JSONArray jsonArray, String tableField) {
        JSONArray array = new JSONArray();

        jsonArray.forEach(e -> {
            JSONObject object = JSONObject.parseObject(e.toString());
            String value = object.getString(tableField);
            Map kv = new JSONObject();
            kv.putAll(object);
            kv.put(object.get("id").toString(), value);
//            kv.put("id", object.get("id").toString());
//            kv.put("name", value);
            array.add(kv);
        });

        return array.toJSONString();
    }

}
